package com.pan.download1;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 *
 * 改进思路
 * 爬虫线程调用调用写入  -> 不成功 -> 爬虫线程阻塞，并加入某个阻塞队列
 *
 * 写入程序结束后查询阻塞队列，如果有下一个则唤醒该线程
 *
 *  让该线程再次调用写入程序
 */


/**
 * 最终敲定的计划是
 *
 * 这个下载限定这个网站
 * www.biquge66.net
 */
public class Download {
    public static void main(String[] args) {

        int corePoolSize = 10;//核心线程数
        int maxPoolSize = 10;//最大线程数
        int queueCapacity = 1;//阻塞队列的大小
        long keepAliveTimr = 1l;//空闲工作线程最大存活时间

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTimr,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(queueCapacity),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

//        String bookName = "三体";
//        String bookUrl = "https://www.biquge66.net/book/92181/";//目录的url

        String bookName = "剑来";
        String bookUrl = "https://www.biquge66.net/book/91/";//目录的url


        Write write = new Write(bookName);

        Mulv mulv = new Mulv();
        mulv.get(bookUrl);
        List<String> urlList = mulv.getUrlList();

        int step = 10;
        for (int i = 0; i < step; i++) {
            Page page = new Page(urlList,i,step,write);
            poolExecutor.execute(page);
        }

        poolExecutor.shutdown();
    }
}
